package com.putprize.percy


import scala.io.Source
import scala.collection.mutable.ArrayBuffer
import net.liftweb.json.{JsonAST,Extraction,Printer,parse}

import scala.collection.mutable.{HashMap=>HMap}

import scala.math.log

object Measure {
  implicit val FORMATS = net.liftweb.json.DefaultFormats
  
  def getData(location:String) = {
    val source = Source.fromFile(location,"UTF-8")
    val lineIterator = source.getLines
    val ms = new ArrayBuffer[(String,Int)]
    for (line <- lineIterator) {
    	  //val x= parse(line)
          val x = line.trim().split("\t")
    	  //val x0 = x.children(0).extract[String]
          val x0 = x(0)
    	  //val x1 = x.children(1).extract[Int]
          val x1 = x(1).toInt
    	  ms += ((x0,x1))
     }
    source.close
    ms.toMap
  }
}

class Scorer (val M:Map[String,Int], val N:Int) {
  
  def score(vs:Array[String]) = {
    val vm = new HMap[String,Int]
    for (v <- vs){
      val c = vm.getOrElse(v, 0)
      vm(v) = c+1
    }
    val s = vm.map(v => {
      val ss = log(v._2+1.0)*log(N.toFloat/M.getOrElse(v._1,1))
      (v._1,ss.toFloat)
    })
    s.toMap
  }
  
  def score(vm:Map[String,Int]) = {
    val s = vm.map(v => {
      val ss = log(v._2+1.0)*log(N.toFloat/M.getOrElse(v._1,N))
      (v._1,ss.toFloat)
    })
    s.toMap
  }

}

